home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / SCRAMBLE.ICN < prev    next >
Text File  |  1992-12-30  |  2KB  |  82 lines

  1. ############################################################################
  2. #
  3. #    File:     scramble.icn
  4. #
  5. #    Subject:  Program to scramble a document
  6. #
  7. #    Author:   Chris Tenaglia
  8. #
  9. #    Date:     May 3, 1992
  10. #
  11. ###########################################################################
  12. #
  13. # This program takes a document and re-outputs it in a cleverly
  14. # scrambled fashion. It uses the next two most likely words to
  15. # to follow.
  16. #
  17. # The concept was found in a recent Scientific American and Icon
  18. # seemed to offer the best implementation.
  19. #
  20. ############################################################################
  21.  
  22. global vocab,index
  23.  
  24. procedure main()
  25.   local line, i, n, word, follows
  26.  
  27.   vocab:= []
  28.   index:= table([])
  29.   while line := read() do
  30.     {
  31.     vocab |||:= parse(line,' ')
  32.     }
  33.  
  34.   every i := 1 to *vocab-2 do index[vocab[i]] |||:= [i]
  35.   index[vocab[-2]] |||:= [-2]    # wrap end to front in order to
  36.   index[vocab[-1]] |||:= [-1]    # prevent stuck loop if last word chosen
  37.  
  38.   n := -1 ; &random := map(&clock,":","0") ; line := ""
  39.   every 1 to *vocab/2 do
  40.     {
  41.     (n > 1) | (n := ?(*vocab-2))
  42.     word    := vocab[n]
  43.     follows := vocab[(?(index[word]))+1]
  44.     n       := (?(index[follows])) + 1
  45.     if (*line + *word + *follows + 2) > 80 then
  46.       {
  47.       write(line)
  48.       line := ""
  49.       }
  50.     line ||:= word || " " || follows || " "
  51.     }
  52.   write(line,".")
  53.   end
  54.  
  55. #
  56. # This procedure pulls all the elements (tokens) out of a line
  57. # buffer and returns them in a list. A variable named chars
  58. # can be statically defined here or global. It is a cset that
  59. # contains the valid characters that can compose the elements
  60. # one wishes to extract.
  61. #
  62.  
  63. procedure parse(line,delims)
  64.   local tokens
  65.   static chars
  66.  
  67.   chars  := &cset -- delims
  68.   tokens := []
  69.   line ? while tab(upto(chars)) do put(tokens,tab(many(chars)))
  70.   return tokens
  71.   end
  72.  
  73. #
  74. # This procedure is terribly handy in prompting and getting
  75. # an input string
  76. #
  77.  
  78. procedure input(prompt)
  79.   writes(prompt)
  80.   return read()
  81.   end
  82.